package com.linkwin.appupdater;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.TextView;

import com.linkwin.updater.AppCheck;
import com.linkwin.updater.UpdaterStyle;
import com.winsafe.libs.base.AppUpdate;
import com.winsafe.libs.utils.UpdaterAppBaseUtil;
import com.winsafe.libs.utils.UpdaterFileUtil;

public class SplashActivity extends AppCompatActivity {

    public final static String APP_NAME = "LinkwinUpdater";
    // APP自动更新检查链接
    public static final String URL_CHECK_VERSION = "http://d.winsafe.cn/apk/UpdateService.ashx?action=%s&appName=%s&appVer=%s&appType=android";

    // app 上线在发布平台的code，提前配置在代码中。不可更改
    public final static String APP_CODE = "linkwinUpdater";
    private String mDownloadUrl = "https://apphub.linkwin.com/ProjectApi/DownloadVersion?projectCode=" + APP_CODE;
    private String mCheckUrl = "https://apphub.linkwin.com/ProjectApi/CheckVersion?projectCode=" + APP_CODE + "&version=";

    private TextView tv_version;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        tv_version = findViewById(R.id.tv_version);
    }

    @Override
    protected void onResume() {
        super.onResume();
        tv_version.setText("当前版本：" + UpdaterAppBaseUtil.getInstance().getAppVersionName(SplashActivity.this));
        requestPermission();
    }


    private void requestPermission() {
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            // 申请READ_PHONE_STATE权限
            ActivityCompat.requestPermissions(this, new String[]{
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
        } else {
//            doUpdate();
            newUpdate();
        }
    }

    /**
     * 旧版本的版本升级
     */
    private void doUpdate() {
        String savePath = UpdaterFileUtil.getSDPath() + "/" + UpdaterAppBaseUtil.getInstance().getAppPackageName(SplashActivity.this);
        String chkVerURL = URL_CHECK_VERSION;
        String saveFileName = APP_NAME;

        chkVerURL = String.format(chkVerURL, "chkver", saveFileName, UpdaterAppBaseUtil.getInstance().getAppVersionName(SplashActivity.this));
        AppUpdate appUpdate = new AppUpdate(SplashActivity.this, chkVerURL, savePath, saveFileName + ".apk");
        appUpdate.checkUpdateInfo(false);
    }

    /**
     * 提供多种配置，如果不需要额外操作，仅需  context,  checkURL, downloadURL 即可完成最基础的更新功能
     */
    private void newUpdate() {
        String checkUrl = mCheckUrl + UpdaterAppBaseUtil.getInstance().getAppVersionName(SplashActivity.this);
        AppCheck appUpdate = new AppCheck(this, checkUrl, mDownloadUrl, UpdaterStyle.CUSTOM_CALLBACK, false, true);
        appUpdate.checkNewVersion();
    }

}